package com.yiwu.web.controller;

import cn.hutool.core.util.ArrayUtil;
import com.yiwu.common.domain.dto.ChatMessageDTO;
import com.yiwu.common.domain.dto.ChatMessageListDTO;
import com.yiwu.common.domain.dto.UserContactDTO;
import com.yiwu.common.domain.entity.ChatMessage;
import com.yiwu.common.domain.entity.ChatSessionUser;
import com.yiwu.common.domain.mapper.ChatMessageMapper;
import com.yiwu.common.enums.MessageTypeEnum;
import com.yiwu.common.enums.ResponseCode;
import com.yiwu.common.exception.BaseResponse;
import com.yiwu.common.exception.BusinessException;
import com.yiwu.common.model.vo.chat.ChatMessageListReqVO;
import com.yiwu.common.model.vo.chat.ChatMessageReqVO;
import com.yiwu.common.utils.ResultUtil;
import com.yiwu.web.service.ChatMessageService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.yiwu.common.utils.EncryptionUtil.getUserToken;

/**
 * @version 1.0
 * @description TODO
 * @date 2024/5/30 0:12
 */
@Slf4j
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Resource
    private ChatMessageService chatMessageService;

    @PostMapping("/send")
    @Operation(summary = "发送消息")
    public BaseResponse<ChatMessage> sendMessage(@RequestBody ChatMessageReqVO chatMessageReqVO, HttpServletRequest request) {
        MessageTypeEnum type = MessageTypeEnum.getType(chatMessageReqVO.getMessageType());
        if (type == null || !ArrayUtil.contains(new Integer[]{MessageTypeEnum.CHAT.getType(), MessageTypeEnum.MEDIA_CHAT.getType()}, chatMessageReqVO.getMessageType())) {
            throw new BusinessException(ResponseCode.MSGTYPE_ERROR);
        }
        Long userId = getUserToken(request);
        ChatMessage chatMessage = new ChatMessage();
        BeanUtils.copyProperties(chatMessageReqVO, chatMessage);
        chatMessage.setSenderId(userId);
        chatMessage.setStatus(1);

        chatMessageService.sendMessage(chatMessage);
        return ResultUtil.success(chatMessage);
    }

    @PostMapping("/contact")
    @Operation(summary = "加好友")
    public BaseResponse<String> contact2User(HttpServletRequest request, @RequestParam String contactId) {
        Long userId = getUserToken(request);
        ChatSessionUser chatSessionUser = new ChatSessionUser();
        chatSessionUser.setContactId(Long.parseLong(contactId));
        chatSessionUser.setUserId(userId);
        chatMessageService.contact2User(chatSessionUser);

        return ResultUtil.success("OK");
    }

    @PostMapping("/msg/list")
    @Operation(summary = "获取当前用户消息列表")
    public BaseResponse<List<ChatMessageListDTO>> getMessageList(@RequestBody ChatMessageListReqVO chatMessageListReqVO, HttpServletRequest request) {
        Long userId = getUserToken(request);
        List<ChatMessageListDTO> chatMessageListDTOList = chatMessageService.getMessageList(chatMessageListReqVO, userId);
        return ResultUtil.success(chatMessageListDTOList);
    }

    @Transactional
    @PostMapping("/get/historical")
    @Operation(summary = "获取历史消息")
    public BaseResponse<List<ChatMessageDTO>> getHistoricalMessageList(@RequestParam String sessionId, @RequestBody ChatMessageListReqVO chatMessageListReqVO, HttpServletRequest request) {
        Long userId = getUserToken(request);
        List<ChatMessageDTO> previousMessageList = chatMessageService.getHistoricalMessageList(sessionId, userId, chatMessageListReqVO);
        return ResultUtil.success(previousMessageList);

    }

    @PostMapping("/get/session")
    @Operation(summary = "点击我想要")
    public BaseResponse<String> getSession(@RequestBody UserContactDTO userContactDTO, HttpServletRequest request) {
        Long userId = getUserToken(request);
        String sessionId = chatMessageService.getSession(userId, userContactDTO);
        return ResultUtil.success(sessionId);
    }

    @Operation(summary = "删除消息列表")
    @PostMapping("/del/list")
    public BaseResponse<Boolean> deleteMessageList(@RequestParam String sessionId, HttpServletRequest request) {
        Long userId = getUserToken(request);
        Boolean res = chatMessageService.deleteMessageList(sessionId, userId.toString());
        return ResultUtil.success(res);
    }

}
